﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;

namespace TSP
{    
    public struct Point
    {
        int _x;
        int _y;
        public int X
        {
            get { return _x; }
            set { _x = value; }
        }        
        public int Y
        {
            get { return _y; }
            set { _y = value; }
        }
        public Point(int x, int y)
        {
            _x = x;
            _y = y;
        }
    }

    public class InOutData
    {
        public static Point[] cities;
        private static int _numOfCity = 1;
        public static int NumOfCity
        {
            get { return InOutData._numOfCity; }
            set { InOutData._numOfCity = value; }
        }
      
        public static string inputFileName()
        {
            string temp = "";
            Console.Write("Nhap ten file: ");
            temp = Console.ReadLine();
            return temp;
        }

        //public static Point[] readFile(string fileName)
        //{
        //    StreamReader doc = new StreamReader(fileName);  
        //    int maxIndex = 1;
        //    Point[] coorTemp;
        //    string line = "";
        //    string pattern = " ";
        //    Regex myRegex = new Regex(pattern);
        //    while ((line = doc.ReadLine()) != null)
        //    {
        //        if (line.IndexOf("DIMENSION") != -1)
        //        {
        //            maxIndex = Convert.ToInt32(line.Substring(line.Length - 2, 2));
        //            break;
        //        }                                
        //    }                 
        //    coorTemp = new Point[maxIndex];
        //    while ((line = doc.ReadLine()) != null)
        //    {               
        //        if (Char.IsNumber(line[0]))
        //        {
        //            Console.WriteLine(line);
        //            string[] temp = myRegex.Split(line);                   
        //            int index = Convert.ToInt32(temp[0]);
        //            coorTemp[index - 1] = new Point(Convert.ToInt32(temp[1]), Convert.ToInt32(temp[2]));
        //            //coor[index-1].X = Convert.ToInt32(temp[1]);
        //            //coor[index-1].Y = Convert.ToInt32(temp[2]);                                       
        //        }                            
        //    }            
        //    doc.Dispose();
        //    return coorTemp;
        //}

        public static void readFile(string fileName)
        {
            StreamReader doc = new StreamReader(fileName);
            int maxIndex = 1;            
            string line = "";
            string pattern = " ";
            Regex myRegex = new Regex(pattern);
            Regex rg = new Regex(@"\s+");
            while ((line = doc.ReadLine()) != null)
            {
                if (line.IndexOf("DIMENSION") != -1)
                {                    
                    maxIndex = Convert.ToInt32(line.Substring(line.LastIndexOf(" "), line.Length - line.LastIndexOf(" ")));                    
                    break;
                }
            }
            cities = new Point[maxIndex];
            _numOfCity = maxIndex;
            while ((line = doc.ReadLine()) != null)
            {
                line = line.Trim();                
                line = rg.Replace(line, " ");
                if (Char.IsNumber(line[0]))
                {                    
                    string[] temp = myRegex.Split(line);                    
                    int index = Convert.ToInt32(temp[0]);                    
                    cities[index - 1] = new Point(Convert.ToInt32(temp[1]), Convert.ToInt32(temp[2]));                    
                }
            }
            doc.Dispose();
            doc.Close();          
        }

        //Tinh khoang cach giua hai thanh pho co chi so trong mang cac thanh pho la: city1St, city2St
        public static float calculateDistance(int city1St, int city2St)
        {            
            return (float)(Math.Sqrt((cities[city1St].X - cities[city2St].X) * (cities[city1St].X - cities[city2St].X) + (cities[city1St].Y - cities[city2St].Y) * (cities[city1St].Y - cities[city2St].Y)));
        }

        public static void writeFile(string fileName, int[] chromo, float fitness, Stopwatch time)
        {
            StreamWriter wr = new StreamWriter(fileName);
            for (int i = 0; i < InOutData.NumOfCity; i++)
            {
                wr.Write(chromo[i] + " ");
            }
            wr.WriteLine();
            wr.WriteLine("Fitness: " + fitness);
            //wr.WriteLine();
            wr.WriteLine("Time: " + time.Elapsed.ToString());
            wr.Close();
        }
    
        //Ghi thong tin ca the tot nhat, tong do thich nghi cua quan the tai moi the he
        public static void writeFitnessInfo(string fileName, ArrayList currentPop, float totalFitness, int repeatStt, Chromosome bestChromosome)
        {
            StreamWriter wr = new StreamWriter(fileName, true);
            try
            {                
                wr.WriteLine(bestChromosome.Fitness + "\t" + totalFitness);            
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                wr.Flush();
                wr.Close();
            }            
        }                 

        //ham tinh goc cua 2 diem p1, p2 so voi phuong ngang
      



    }
}
